package com.test;

import com.bean.Dao.UserMapper;
import com.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class UserTest3 {
    @Test
    public void test01() throws Exception {
        //1.加载核心配置文件 - 编译后存在根目录下
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取sqlSession工厂对象
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        //3.获取会话对象
        SqlSession ss = ssf.openSession(true);

        //4.获取UserMapper接口的实现类对象
        //等同于 UserDaoImpl
        UserMapper mapper = ss.getMapper(UserMapper.class);
        //5.调用UserMapper中的方法
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        System.out.println("---------------------");
        User u = mapper.findById(2);
        System.out.println(u);

    }
}
